<!--
库存销量分析
 -->
<template>
    <div class="com-container">
        <div class="com-chart" ref="stock_ref"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      charInstance: null,
      allData: null, // 服务器返回的数据
      currentIndex: 0, // 当前显示的页数
      timeId: null, // 定时器标识
      titleFontSize: 0
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      // console.log('主题切换了')
      this.charInstance.dispose() // 销毁当前图标
      this.initChart() // 重新以最新的主体名称初始化对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  },
  created () {
    this.$socket.registerCallBack('stockData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock'
    })
    window.addEventListener('resize', this.screenAdapter)
    // 在页面加载完成的时候  主动进行屏幕的适配
    this.screenAdapter()
  },
  destroyed () {
    // 在组件销毁的时候 需要把适配事件注销  防止内存泄漏
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timeId)
    this.$socket.unRegisterCallBack('stockData')
  },
  methods: {
    // 初始化echartsInstance对象
    async initChart () {
      this.charInstance = this.$echarts.init(this.$refs.stock_ref, this.theme)
      // 对图表初始化配置的控制
      const initOption = {
        title: {
          text: '▍ 库存和销量分析',
          left: 20,
          top: 20
        }
      }
      this.charInstance.setOption(initOption)
      this.charInstance.on('mouseover', () => {
        clearInterval(this.timeId)
      })
      this.charInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取服务器的数据
    async getData (ret) {
      // http://localhost:8888/api/stock
      // const { data: ret } = await this.$http.get('stock')
      this.allData = ret
      this.updateChart()
      this.startInterval()
    },
    // 更新图表
    updateChart () {
      // 增加5个圆环的渐变颜色范围
      const colorArrs = [
        ['#4FF778', '#0BA82C'], ['#E5DD45', '#E8B11C'], ['#E8821C', '#E55445'], ['#5052EE', '#AB6EE5'], ['#23E5E5', '#2E72BF']
      ]
      const centerArr = [
        ['18%', '40%'], ['50%', '40%'], ['82%', '40%'], ['34%', '75%'], ['66%', '75%']
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          radius: [this.titleFontSize * 2.8 * 1.125, this.titleFontSize * 2.8],
          center: centerArr[index],
          hoverAnimation: false, // 鼠标经过圆环 取消动画效果
          labelLine: [
            {
              show: false // 隐藏指示线
            }
          ],
          label: {
            position: 'center',
            color: colorArrs[index][0],
            fontSize: this.titleFontSize / 2
          },
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArrs[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArrs[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })
      const dataOption = {
        series: seriesArr
      }
      this.charInstance.setOption(dataOption)
    },
    // 当浏览器的大小发生变化的时候 调用 完成分辨率适配
    screenAdapter () {
      this.titleFontSize = this.$refs.stock_ref.offsetWidth / 100 * 3.6
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        }
      }
      this.charInstance.setOption(adapterOption)
      // 手动调用图标对象的resize  才可以产生效果
      this.charInstance.resize()
    },
    startInterval () {
      if (this.timeId) {
        clearInterval(this.timeId)
      }
      this.timeId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.updateChart() // 在更改完currentIndex 之后 更新数据
      }, 5000)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
